package cn.byapps.hw.controller;

import cn.byapps.hw.dto.TeacherDTO;
import cn.byapps.hw.entity.Teacher;
import cn.byapps.hw.service.TeacherService;
import cn.byapps.hw.utils.MD5Utils;
import cn.byapps.hw.utils.RandomValidateCodeUtil;
import cn.byapps.hw.utils.RedisUtil;
import cn.byapps.hw.vo.TeacherVO;
import cn.byapps.hw.vo.VueData;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/teacher")
public class TeacherController {

    @Resource
    private TeacherService teacherService;
    @Resource
    private RedisUtil redisUtil;

    @GetMapping
    public VueData<TeacherVO> teacherVOS(Integer pos, Integer rows, String teacherId) {
        int total = 0;
        List<TeacherVO> teacherVOS;
        if (pos == 0 && rows == 0 && teacherId == null) {
            return new VueData<>(teacherService.findVO(), 0);
        }
        if (teacherId == null) {
            total = teacherService.count();
            teacherVOS = teacherService.findVO(pos, rows);
        } else {
            teacherVOS = teacherService.findVOByTeacher(pos, rows, teacherId);
            total = teacherVOS.size();
        }
        VueData<TeacherVO> vue = new VueData<>(teacherVOS, total);
        return vue;
    }

    @PostMapping
    public VueData<Teacher> add(@RequestBody Teacher teacher) {
        teacherService.add(teacher);
        return new VueData<Teacher>();
    }

    @DeleteMapping
    public VueData<Teacher> delete(String id) {
        teacherService.deleteById(id);
        return new VueData<Teacher>();
    }

    @DeleteMapping("deleteCourse")
    public VueData<Teacher> deleteCourse(String teacherId, Integer courseId, Integer clazzId) {
        teacherService.deleteCourse(teacherId, courseId, clazzId);
        return new VueData<Teacher>();
    }

    @PutMapping
    public VueData<Teacher> update(@RequestBody Teacher Teacher) {
        teacherService.update(Teacher);
        return new VueData<Teacher>();
    }

    /**
     * 生成验证码
     */
    @RequestMapping(value = "/getVerify")
    public void getVerify(HttpServletRequest request, HttpServletResponse response,HttpSession session) {
        try {
            //设置相应类型,告诉浏览器输出的内容为图片
            response.setContentType("image/jpeg");
            //设置响应头信息，告诉浏览器不要缓存此内容
            response.setHeader("Pragma", "No-cache");
            response.setHeader("Cache-Control", "no-cache");
            response.setDateHeader("Expire", 0);
            RandomValidateCodeUtil randomValidateCode = new RandomValidateCodeUtil();
            //输出验证码图片方法
            randomValidateCode.getRandcode(request, response);
        } catch (Exception e) {
            System.out.println(e);
        }
        redisUtil.set((String) session.getAttribute("RANDOMVALIDATECODEKEY"),(String) session.getAttribute("RANDOMVALIDATECODEKEY"),60);
    }

    @PostMapping(value = "/checkVerify")
    public boolean checkVerify(TeacherDTO teacherDTO) {
        if (redisUtil.hasKey(teacherDTO.getVerify()) == true && teacherService.getByNameAndPassword(new Teacher(null,teacherDTO.getId(), MD5Utils.md5(teacherDTO.getPassword()),null)) != null){
            return true;
        }
        return false;
    }

}
